package cn.stylefeng.guns.modular.acpr.controller;

import cn.stylefeng.roses.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;

import cn.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import cn.stylefeng.guns.modular.system.model.Acpr;
import cn.stylefeng.guns.modular.acpr.service.IAcprService;

/**
 * 书刊类型控制器
 *
 * @author fengshuonan
 * @Date 2020-06-20 22:04:25
 */
@Controller
@RequestMapping("/acpr")
public class AcprController extends BaseController {

    private String PREFIX = "/acpr/acpr/";

    @Autowired
    private IAcprService acprService;

    /**
     * 跳转到书刊类型首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "acpr.html";
    }

    /**
     * 跳转到添加书刊类型
     */
    @RequestMapping("/acpr_add")
    public String acprAdd() {
        return PREFIX + "acpr_add.html";
    }

    /**
     * 跳转到修改书刊类型
     */
    @RequestMapping("/acpr_update/{acprId}")
    public String acprUpdate(@PathVariable Integer acprId, Model model) {
        Acpr acpr = acprService.selectById(acprId);
        model.addAttribute("item",acpr);
        LogObjectHolder.me().set(acpr);
        return PREFIX + "acpr_edit.html";
    }

    /**
     * 获取书刊类型列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return acprService.selectList(null);
    }

    /**
     * 新增书刊类型
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Acpr acpr) {
        acprService.insert(acpr);
        return SUCCESS_TIP;
    }

    /**
     * 删除书刊类型
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer acprId) {
        acprService.deleteById(acprId);
        return SUCCESS_TIP;
    }

    /**
     * 修改书刊类型
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Acpr acpr) {
        acprService.updateById(acpr);
        return SUCCESS_TIP;
    }

    /**
     * 书刊类型详情
     */
    @RequestMapping(value = "/detail/{acprId}")
    @ResponseBody
    public Object detail(@PathVariable("acprId") Integer acprId) {
        return acprService.selectById(acprId);
    }
    
    @InitBinder
	public void initBinder(WebDataBinder binder) {
	    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));        
	}
}
